package com.spring.controller;

import com.spring.model.UserInfo;
import com.spring.util.MyNewUploadFile;
import com.spring.util.MyUploadFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public ModelAndView Default(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

    /**
     * 上传页面
     * */
    @RequestMapping(value = "upload")
    public ModelAndView upload(){
        ModelAndView mv = new ModelAndView("upload");
        return mv;
    }

    /**
     * 文件上传处理请求
     * */
    @RequestMapping(value = "uploadHandler")
    public String uploadFile(HttpServletRequest request) throws IOException {
        // 设置我的上传路径
        String uploadpath = "upload\\img";
        // 设置允许上传的扩展名
        String[] filetype = { ".jpg", ".png" };

        //
        List<String> filelist = MyUploadFile.FileUpload(request, uploadpath, filetype);

        if(filelist!=null){
            for (String s : filelist) {
                System.out.println("上传成功");
                System.out.println(s);
            }
            return "redirect:/success";
        }else{
            return "redirect:/error";
        }
    }

    /**
     * 上传成功页面
     * */
    @RequestMapping("/success")
    public ModelAndView success(){
        ModelAndView mv=new ModelAndView("success");
        return mv;
    }

    /**
     * 上传失败页面
     * */
    @RequestMapping("/error")
    public ModelAndView error(){
        ModelAndView mv=new ModelAndView("error");
        return mv;
    }

    /**
     * 带表单的文件上传
     * */
    @RequestMapping(value = "uploadform")
    public ModelAndView uploadform(){
        ModelAndView mv = new ModelAndView("uploadform");
        return mv;
    }

    /**
     * 带表单文件上传
     * */
    @PostMapping(value = "uploadFormHandler")
    public String uploadFormHandler(UserInfo userInfo, HttpServletRequest request){
        System.out.println("username:"+userInfo.getUsername());
        return "success";
    }

    /**
     * 表单提交页面
     * */
    @RequestMapping(value = "addform")
    public ModelAndView addform(){
        ModelAndView mv = new ModelAndView("addform");
        return mv;
    }

    /**
     * addFormHandler 提交
     * */
    @RequestMapping(value = "addFormHandler")
    public String addFormHandler(UserInfo userInfo,MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("user:"+userInfo);
        // 设置我的上传路径
        String uploadpath = "upload\\img";
        // 设置允许上传的扩展名
        String[] filetype = { ".jpg", ".png" };

        //
        String fileUploadPath = MyNewUploadFile.uploadSingleFile(file,uploadpath,filetype,request);

        if(fileUploadPath!=null){
            userInfo.setPhoto(fileUploadPath);
            System.out.println("提交到后端数据库的对象："+userInfo);
            return "redirect:/success";
        }else{
            return "redirect:/error";
        }
    }

    /**
     * 多文件上传页面
     * */
    @RequestMapping(value = "addmultifileform")
    public ModelAndView addmultifileform(){
        ModelAndView mv=new ModelAndView("addmultifileform");
        return mv;
    }

    /**
     * 处理多文件上传请求方法
     * addFilesHandler
     * */
    @RequestMapping(value = "addFilesHandler")
    public String addFilesHandler(UserInfo userInfo,MultipartHttpServletRequest multiRequest, HttpServletRequest request) throws IOException {
        System.out.println(userInfo);
        System.out.println(multiRequest.getFiles("headImg"));
        System.out.println(multiRequest.getFiles("bgImg"));

        // 设置我的上传路径
        String uploadpath = "upload\\img";
        // 设置允许上传的扩展名
        String[] filetype = { ".jpg", ".png" };

        //获取指定name的文件标签
        List<MultipartFile> headImgList = multiRequest.getFiles("headImg");
        List<MultipartFile> bgImgList = multiRequest.getFiles("bgImg");

        //多文件上传就是for循环调用单文件依次上传，直到所有文件上传完毕
        String user_bgimg = "";
        String user_headimg = "";
        for (MultipartFile file : bgImgList) {
            user_bgimg = MyNewUploadFile.uploadSingleFile(file,uploadpath,filetype,request);
        }
        for (MultipartFile file : headImgList) {
            user_headimg = MyNewUploadFile.uploadSingleFile(file,uploadpath,filetype,request);
        }
        //这里用set方法将多张图片放入
        //这里仅演示
        System.out.println(user_bgimg);
        System.out.println(user_headimg);
        //完成

        return "success";
    }
}
